import {commonLogin} from './login.js'
const appid = '';

// 用户登录
export default function userLogin(){
	let type = 1;
	return new Promise(resolve => {
		let user = uni.getStorageSync('user');
		if(user == "" || user == undefined || user == null){
			//获取登录服务提供商
			uni.getProvider({
			    service: 'oauth',
			    success: function (res) {
			        console.log('res.provider',res.provider)
			        if (res.provider && res.provider.length>0) {
								let provider = res.provider[0];		//登录服务提供商
								//登录
			            uni.login({
			                provider: provider,
			                success: function (loginRes) {
			                    console.log('loginRes',loginRes);
								try{
									//远程登录
									commonLogin(provider,{appid,code: loginRes.code,type}).then(res=>{
										if(res.data.code == 200 && type == 0){
											console.log('111111111',res.data);
											uni.setStorageSync('uid', res.data.data.id);
											uni.setStorageSync('user',  res.data.data);
											if(res.data.session_key){
												uni.setStorageSync('session_key', res.data.session_key);	
											}
										}
										resolve(res);
									});
								}catch(e){
									console.error(e);
								}
			                }
			            });
			        }
			    }
			});
		}else{
			resolve({
				data: {
					code: 200,
					data: user,
					msg: ''
				}
			});
		}
		
	})
	
}